library(haven)
library(survey)
library(tidyverse)
library(dplyr)
library(ggplot2)

library(ltm) # For vanilla IRT models

library(margins)
library(effects)

library(hIRT) # For hierarchical IRT models

library(nnet) # for multinomial logit
library(svyVGAM)

library(boot) # for estimating CIs of svy_vglm models


# data_dir <- "Downloads/reexternalsurvey"
data_dir <- "."

caltech_climate_junejuly24 <- read_sav(file.path(data_dir, "caltech_climate_junejuly24.sav"))
# View(caltech_climate_junejuly24)

#Initial recodes for tables
dat <- caltech_climate_junejuly24 %>%
    mutate(
        gender2 = case_when(gender4 == 1 ~ 1,
                            gender4 == 2 ~ 2),
        urbancity4 = case_when(urbancity == 1 ~ 1,
                               urbancity == 2 ~ 2,
                               urbancity == 3 ~ 3,
                               urbancity == 4 ~ 4),
        Q26_3 = na_if(Q26_3, 8),
        Q26_4 = na_if(Q26_4, 8),
        Q26_5 = na_if(Q26_5, 8),
        Q26_6 = na_if(Q26_6, 8),
        # reverse populism so that 1 is strongly disagree and 5 strongly agree (so 5 is high populism)
        populism_1 = 6 - populism_1,
        populism_2 = 6 - populism_2,
        populism_3 = 6 - populism_3,
        populism_4 = 6 - populism_4,
        populism_5 = 6 - populism_5,
        populism_6 = 6 - populism_6,
        # code law questions to be on 3-point scale
        law1_2 = case_when(law_1 == 1 | law_1 == 2 ~ 1,
                           law_1 == 3 | law_1 == 4 ~ 2,
                           law_1 == 5 ~ 3),
        law2_2 = case_when(law_2 == 1 | law_2 == 2 ~ 1,
                           law_2 == 3 | law_2 == 4 ~ 2,
                           law_2 == 5 ~ 3),
        law3_2 = case_when(law_3 == 1 | law_3 == 2 ~ 1,
                           law_3 == 3 | law_3 == 4 ~ 2,
                           law_3 == 5 ~ 3),
        law4_2 = case_when(law_4 == 1 | law_4 == 2 ~ 1,
                           law_4 == 3 | law_4 == 4 ~ 2,
                           law_4 == 5 ~ 3),
        law5_2 = case_when(law_5 == 1 | law_5 == 2 ~ 1,
                           law_5 == 3 | law_5 == 4 ~ 2,
                           law_5 == 5 ~ 3),
        law6_2 = case_when(law_6 == 1 | law_6 == 2 ~ 1,
                           law_6 == 3 | law_6 == 4 ~ 2,
                           law_6 == 5 ~ 3),
        law7_2 = case_when(law_7 == 1 | law_7 == 2 ~ 1,
                           law_7 == 3 | law_7 == 4 ~ 2,
                           law_7 == 5 ~ 3),
        law8_2 = case_when(law_8 == 1 | law_8 == 2 ~ 1,
                           law_8 == 3 | law_8 == 4 ~ 2,
                           law_8 == 5 ~ 3),
        law9_2 = case_when(law_9 == 1 | law_9 == 2 ~ 1,
                           law_9 == 3 | law_9 == 4 ~ 2,
                           law_9 == 5 ~ 3),
        law10_2 = case_when(law_10 == 1 | law_10 == 2 ~ 1,
                            law_10 == 3 | law_10 == 4 ~ 2,
                            law_10 == 5 ~ 3),
        law1_3 = case_when(law_1 == 1 | law_1 == 2 ~ 1,
                           law_1 == 3 | law_1 == 4 ~ 0),
        law2_3 = case_when(law_2 == 1 | law_2 == 2 ~ 1,
                           law_2 == 3 | law_2 == 4 ~ 0),
        law3_3 = case_when(law_3 == 1 | law_3 == 2 ~ 1,
                           law_3 == 3 | law_3 == 4 ~ 0),
        law4_3 = case_when(law_4 == 1 | law_4 == 2 ~ 1,
                           law_4 == 3 | law_4 == 4 ~ 0),
        law5_3 = case_when(law_5 == 1 | law_5 == 2 ~ 1,
                           law_5 == 3 | law_5 == 4 ~ 0),
        law6_3 = case_when(law_6 == 1 | law_6 == 2 ~ 1,
                           law_6 == 3 | law_6 == 4 ~ 0),
        law7_3 = case_when(law_7 == 1 | law_7 == 2 ~ 1,
                           law_7 == 3 | law_7 == 4 ~ 0),
        law8_3 = case_when(law_8 == 1 | law_8 == 2 ~ 1,
                           law_8 == 3 | law_8 == 4 ~ 0),
        law9_3 = case_when(law_9 == 1 | law_9 == 2 ~ 1,
                           law_9 == 3 | law_9 == 4 ~ 0),
        law10_3 = case_when(law_10 == 1 | law_10 == 2 ~ 1,
                            law_10 == 3 | law_10 == 4 ~ 0)
        )


# this takes the average of the 6 populism questions and creates a standardized measure 0 to 1 rather than 1 to 5.
dat <- 
  dat %>%
  rowwise() %>%
  dplyr::mutate(populism_avg = mean(c(populism_1, populism_2, populism_3, 
                                      populism_4, populism_5, populism_6), na.rm=F),
                populism = (populism_avg - 1) / (5-1))


dat <- dat %>%
  mutate(conspr_b1_q1 = Q26_1,
            conspr_b1_q2 = Q26_2,
            conspr_b1_q3 = Q26_3,
            conspr_b1_q4 = Q26_4,
            conspr_b1_q5 = Q26_5,
         conspr_b1_q6 = Q26_6)

conspiracy_total <- dat %>%
  dplyr::select(starts_with("conspr_")) %>%
  rowSums()

dat$conspiracy_total <- conspiracy_total

# Estimate IRT model of conspiracy beliefs and climate knowledge:
conspiracy_IRT <- grm(data = dat %>% dplyr::select(starts_with("conspr_")))

# Extract IRT latent scores based on the IRT models:
conspiracy_scores <- ltm::factor.scores(conspiracy_IRT)$score.dat %>%
  mutate(conspiracy_score = (z1 - mean(z1))/sd(z1))

# Append IRT latent scores to survey responses:
dat_appended <- dat %>%
  left_join(conspiracy_scores %>% dplyr::select(starts_with("conspr_"), conspiracy_score),
            by = c("conspr_b1_q1", "conspr_b1_q2", "conspr_b1_q3", "conspr_b1_q4", "conspr_b1_q5",
                   "conspr_b1_q6")) 

dat_appended <- dat_appended %>%
  mutate(across(c(age4, gender2, race4, educ4, pid3, ideo3, region, urbancity4), as.factor),
         law1_2 = as.factor(law1_2))

#Trying PCA for the conspiracy theories
cols_PCA <- c("Q26_1", "Q26_2", "Q26_3", "Q26_4", "Q26_5", "Q26_6")
data_complete <- na.omit(dat_appended[,cols_PCA])
conspiracy_pca <- prcomp(data_complete[,cols_PCA])
conspiracy_score_all <- predict(conspiracy_pca, newdata=dat_appended)
conspiracy_score <- conspiracy_score_all[,1]
conspiracy_score_total <- dat_appended %>% dplyr::select(all_of(cols_PCA)) %>% rowSums()
dat_appended$conspiracy_score_total <- conspiracy_score_total

# First, let's identify rows with missing values in any of the variables used in the model
variables_in_model <- c("law1_2", "age4", "gender2", "race4", "educ4", "pid3", "ideo3", 
                        "region", "urbancity4", "populism", "conspiracy_score")
complete_cases <- complete.cases(dat_appended[,variables_in_model])
dat_appended <- dat_appended %>% 
  mutate(
    law1_2 = as.factor(law1_2),
    age4 = as.factor(age4),
    gender2 = as.factor(gender2),
    race4 = as.factor(race4),
    educ4 = as.factor(educ4),
    pid3 = as.factor(pid3),
    ideo3 = as.factor(ideo3),
    region = as.factor(region),
    urbancity4 = as.factor(urbancity4),
    populism = as.numeric(populism),
    conspiracy_score = as.numeric(conspiracy_score)
  )
dat_final <- subset(dat_appended, complete_cases)

#Apply survey weights
national.design.climate.july24= svydesign(data = dat_final, weights = ~weight, id = ~1)

#Election Crime toplines
# Labels of what these mean:
law_rating_names <- c("Very Common","Occassionally","Infrequently","Never", "Not Sure")
law1_prop <- round(prop.table(svytable(~law_1, national.design.climate.july24)),2) %>% as.numeric()
law2_prop <- round(prop.table(svytable(~law_2, national.design.climate.july24)),2) %>% as.numeric()
law3_prop <- round(prop.table(svytable(~law_3, national.design.climate.july24)),2) %>% as.numeric()
law4_prop <- round(prop.table(svytable(~law_4, national.design.climate.july24)),2) %>% as.numeric()
law5_prop <- round(prop.table(svytable(~law_5, national.design.climate.july24)),2) %>% as.numeric()
law6_prop <- round(prop.table(svytable(~law_6, national.design.climate.july24)),2) %>% as.numeric()
law7_prop <- round(prop.table(svytable(~law_7, national.design.climate.july24)),2) %>% as.numeric()
law8_prop <- round(prop.table(svytable(~law_8, national.design.climate.july24)),2) %>% as.numeric()
law9_prop <- round(prop.table(svytable(~law_9, national.design.climate.july24)),2) %>% as.numeric()
law10_prop <- round(prop.table(svytable(~law_10, national.design.climate.july24)),2) %>% as.numeric()

#Binary with NS
round(prop.table(svytable(~law1_2, national.design.climate.july24)),2)
round(prop.table(svytable(~law2_2, national.design.climate.july24)),2)
round(prop.table(svytable(~law3_2, national.design.climate.july24)),2)
round(prop.table(svytable(~law4_2, national.design.climate.july24)),2)
round(prop.table(svytable(~law5_2, national.design.climate.july24)),2)
round(prop.table(svytable(~law6_2, national.design.climate.july24)),2)
round(prop.table(svytable(~law7_2, national.design.climate.july24)),2)
round(prop.table(svytable(~law8_2, national.design.climate.july24)),2)
round(prop.table(svytable(~law9_2, national.design.climate.july24)),2)
round(prop.table(svytable(~law10_2, national.design.climate.july24)),2)

#Binary without NS
round(prop.table(svytable(~law1_3, national.design.climate.july24)),2)
round(prop.table(svytable(~law2_3, national.design.climate.july24)),2)
round(prop.table(svytable(~law3_3, national.design.climate.july24)),2)
round(prop.table(svytable(~law4_3, national.design.climate.july24)),2)
round(prop.table(svytable(~law5_3, national.design.climate.july24)),2)
round(prop.table(svytable(~law6_3, national.design.climate.july24)),2)
round(prop.table(svytable(~law7_3, national.design.climate.july24)),2)
round(prop.table(svytable(~law8_3, national.design.climate.july24)),2)
round(prop.table(svytable(~law9_3, national.design.climate.july24)),2)
round(prop.table(svytable(~law10_3, national.design.climate.july24)),2)

#All of the frequencies are with the binary outcomes (law*_3)

#PID3
round(prop.table(svytable(~pid3, national.design.climate.july24)),2)

#Row frequencies
round(prop.table(svytable(~pid3+law1_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~pid3+law2_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~pid3+law3_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~pid3+law4_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~pid3+law5_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~pid3+law6_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~pid3+law7_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~pid3+law8_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~pid3+law9_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~pid3+law10_3, national.design.climate.july24),1),2)

#Column frequencies
round(prop.table(svytable(~pid3+law1_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~pid3+law2_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~pid3+law3_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~pid3+law4_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~pid3+law5_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~pid3+law6_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~pid3+law7_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~pid3+law8_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~pid3+law9_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~pid3+law10_3, national.design.climate.july24),2),2)

#IDEO3
round(prop.table(svytable(~ideo3, national.design.climate.july24)),2)

#Row frequencies
round(prop.table(svytable(~ideo3+law1_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~ideo3+law2_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~ideo3+law3_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~ideo3+law4_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~ideo3+law5_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~ideo3+law6_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~ideo3+law7_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~ideo3+law8_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~ideo3+law9_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~ideo3+law10_3, national.design.climate.july24),1),2)

#Column frequencies
round(prop.table(svytable(~ideo3+law1_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~ideo3+law2_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~ideo3+law3_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~ideo3+law4_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~ideo3+law5_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~ideo3+law6_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~ideo3+law7_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~ideo3+law8_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~ideo3+law9_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~ideo3+law10_3, national.design.climate.july24),2),2)

#AGE4
round(prop.table(svytable(~age4, national.design.climate.july24)),2)

#Row frequencies
round(prop.table(svytable(~age4+law1_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~age4+law2_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~age4+law3_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~age4+law4_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~age4+law5_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~age4+law6_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~age4+law7_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~age4+law8_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~age4+law9_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~age4+law10_3, national.design.climate.july24),1),2)

#Column frequencies
round(prop.table(svytable(~age4+law1_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~age4+law2_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~age4+law3_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~age4+law4_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~age4+law5_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~age4+law6_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~age4+law7_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~age4+law8_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~age4+law9_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~age4+law10_3, national.design.climate.july24),2),2)

#REGION
round(prop.table(svytable(~region, national.design.climate.july24)),2)

#Row frequencies
round(prop.table(svytable(~region+law1_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~region+law2_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~region+law3_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~region+law4_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~region+law5_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~region+law6_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~region+law7_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~region+law8_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~region+law9_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~region+law10_3, national.design.climate.july24),1),2)

#Column frequencies
round(prop.table(svytable(~region+law1_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~region+law2_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~region+law3_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~region+law4_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~region+law5_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~region+law6_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~region+law7_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~region+law8_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~region+law9_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~region+law10_3, national.design.climate.july24),2),2)

#URBANCITY4
round(prop.table(svytable(~urbancity4, national.design.climate.july24)),2)

#Row frequencies
round(prop.table(svytable(~urbancity4+law1_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~urbancity4+law2_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~urbancity4+law3_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~urbancity4+law4_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~urbancity4+law5_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~urbancity4+law6_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~urbancity4+law7_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~urbancity4+law8_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~urbancity4+law9_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~urbancity4+law10_3, national.design.climate.july24),1),2)

#Column frequencies
round(prop.table(svytable(~urbancity4+law1_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~urbancity4+law2_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~urbancity4+law3_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~urbancity4+law4_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~urbancity4+law5_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~urbancity4+law6_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~urbancity4+law7_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~urbancity4+law8_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~urbancity4+law9_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~urbancity4+law10_3, national.design.climate.july24),2),2)


#GENDER2
round(prop.table(svytable(~gender2, national.design.climate.july24)),2)

#Row frequencies
round(prop.table(svytable(~gender2+law1_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~gender2+law2_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~gender2+law3_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~gender2+law4_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~gender2+law5_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~gender2+law6_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~gender2+law7_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~gender2+law8_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~gender2+law9_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~gender2+law10_3, national.design.climate.july24),1),2)

#Column frequencies
round(prop.table(svytable(~gender2+law1_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~gender2+law2_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~gender2+law3_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~gender2+law4_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~gender2+law5_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~gender2+law6_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~gender2+law7_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~gender2+law8_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~gender2+law9_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~gender2+law10_3, national.design.climate.july24),2),2)

#RACE4
round(prop.table(svytable(~race4, national.design.climate.july24)),2)

#Row frequencies
round(prop.table(svytable(~race4+law1_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~race4+law2_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~race4+law3_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~race4+law4_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~race4+law5_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~race4+law6_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~race4+law7_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~race4+law8_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~race4+law9_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~race4+law10_3, national.design.climate.july24),1),2)

#Column frequencies
round(prop.table(svytable(~race4+law1_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~race4+law2_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~race4+law3_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~race4+law4_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~race4+law5_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~race4+law6_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~race4+law7_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~race4+law8_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~race4+law9_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~race4+law10_3, national.design.climate.july24),2),2)

#EDUC4
round(prop.table(svytable(~educ4, national.design.climate.july24)),2)

#Row frequencies
round(prop.table(svytable(~educ4+law1_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~educ4+law2_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~educ4+law3_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~educ4+law4_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~educ4+law5_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~educ4+law6_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~educ4+law7_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~educ4+law8_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~educ4+law9_3, national.design.climate.july24),1),2)
round(prop.table(svytable(~educ4+law10_3, national.design.climate.july24),1),2)

#Column frequencies
round(prop.table(svytable(~educ4+law1_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~educ4+law2_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~educ4+law3_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~educ4+law4_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~educ4+law5_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~educ4+law6_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~educ4+law7_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~educ4+law8_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~educ4+law9_3, national.design.climate.july24),2),2)
round(prop.table(svytable(~educ4+law10_3, national.design.climate.july24),2),2)

#Topline graphs

law_1_data <- data.frame(Likelihood=law_rating_names,Frequency = law1_prop)
law1 <- ggplot(law_1_data, aes(x=Likelihood, y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Voting More Than Once") + geom_text(aes(label=Frequency), hjust=2) + scale_y_continuous(limits=c(0,.5))

law_2_data <- data.frame(Likelihood=law_rating_names,Frequency = law2_prop)
law2 <- ggplot(law_2_data, aes(x=Likelihood, y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Ballot Tampering") + geom_text(aes(label=Frequency), hjust=2) + scale_y_continuous(limits=c(0,.5))

law_3_data <- data.frame(Likelihood=law_rating_names,Frequency = law3_prop)
law3 <- ggplot(law_3_data, aes(x=Likelihood, y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Voter Impersonation") + geom_text(aes(label=Frequency), hjust=2) + scale_y_continuous(limits=c(0,.5))

law_4_data <- data.frame(Likelihood=law_rating_names,Frequency = law4_prop)
law4 <- ggplot(law_4_data, aes(x=Likelihood, y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Non-Citizen Voting") + geom_text(aes(label=Frequency), hjust=2) + scale_y_continuous(limits=c(0,.5))

law_5_data <- data.frame(Likelihood=law_rating_names,Frequency = law5_prop)
law5 <- ggplot(law_5_data, aes(x=Likelihood, y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Mail Voting Fraud") + geom_text(aes(label=Frequency), hjust=2) + scale_y_continuous(limits=c(0,.5))

law_6_data <- data.frame(Likelihood=law_rating_names,Frequency = law6_prop)
law6 <- ggplot(law_6_data, aes(x=Likelihood, y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Tampering With Results") + geom_text(aes(label=Frequency), hjust=2) + scale_y_continuous(limits=c(0,.5))

law_7_data <- data.frame(Likelihood=law_rating_names,Frequency = law7_prop)
law7 <- ggplot(law_7_data, aes(x=Likelihood, y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Software Manipulation") + geom_text(aes(label=Frequency), hjust=2) + scale_y_continuous(limits=c(0,.5))

law_8_data <- data.frame(Likelihood=law_rating_names,Frequency = law8_prop)
law8 <- ggplot(law_8_data, aes(x=Likelihood, y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Paying For Votes") + geom_text(aes(label=Frequency), hjust=2) + scale_y_continuous(limits=c(0,.5))

law_9_data <- data.frame(Likelihood=law_rating_names,Frequency = law9_prop)
law9 <- ggplot(law_9_data, aes(x=Likelihood, y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Voter Registration Fraud") + geom_text(aes(label=Frequency), hjust=2) + scale_y_continuous(limits=c(0,.5))

law_10_data <- data.frame(Likelihood=law_rating_names,Frequency = law10_prop)
law10 <- ggplot(law_9_data, aes(x=Likelihood, y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Dropbox Stuffing") + geom_text(aes(label=Frequency), hjust=2) + scale_y_continuous(limits=c(0,.5))

Crime <- c("Mail Ballot Fraud", "Dropbox Fraud", "Paying Voters", "Non-Citizen Voting", "Registration Fraud", "Impersonation", "Software Hacking", "Multiple Voting", "Ballot Tampering", "Official Tampering")
Frequency <- c(.43, .41, .41, .40, .37, .35, .35, .34, .33, .32)
crimes <- data.frame(Crime,Frequency)

crimes_bar <- ggplot(crimes, aes(x=reorder(Crime, Frequency), y=Frequency)) + geom_bar(stat = "identity") + coord_flip() + ggtitle("Concerns About Election Crimes") + geom_text(aes(label=Frequency), hjust=2, colour = "white") + scale_y_continuous(limits=c(0,.50)) + labs(x="Election Crimes")

#Quantities for survey methods table

method1a <- round(prop.table(table(dat_final$age4)),2)
method1b <- round(prop.table(svytable(~age4, national.design.climate.july24)),2)

method2a <- round(prop.table(table(dat_final$gender2)),2)
method2b <- round(prop.table(svytable(~gender2, national.design.climate.july24)),2)

method3a <- round(prop.table(table(dat_final$race4)),2)
method3b <- round(prop.table(svytable(~race4, national.design.climate.july24)),2)

method4a <- round(prop.table(table(dat_final$educ4)),2)
method4b <- round(prop.table(svytable(~educ4, national.design.climate.july24)),2)

method5a <- round(prop.table(table(dat_final$pid3)),2)
method5b <- round(prop.table(svytable(~pid3, national.design.climate.july24)),2)

method6a <- round(prop.table(table(dat_final$ideo3)),2)
method6b <- round(prop.table(svytable(~ideo3, national.design.climate.july24)),2)

method7a <- round(prop.table(table(dat_final$region)),2)
method7b <- round(prop.table(svytable(~region, national.design.climate.july24)),2)

method8a <- round(prop.table(table(dat_final$urbancity4)),2)
method8b <- round(prop.table(svytable(~urbancity4, national.design.climate.july24)),2)
